package com.mcms.service.dw;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.ralasafe.user.User;

import com.mcms.util.CalendarUtil;
import com.mcms.util.DBHelper;
import com.mcms.util.GetParamsUtil;
import com.mcms.util.McmsApiConfig;

/**
 * 将客户基本信息插入数据库
 * 
 * @since 2012-7-14
 * @see
 * version 1.0.0 2012-7-14
 */
public class AddCustomBasicInfoService {

	public static boolean execute(HttpServletRequest request,String uuid){
		
		boolean isSuc=true;
		
		//先通过customId查询该客户是否存在
		boolean isAdd = queryByCustomId(uuid);
		
		//用户不存在执行insert操作，否则执行update操作
		if(isAdd){
			isSuc = updateCustomInfo(request,uuid);
		} else {
			isSuc = addCustomInfo(request,uuid);
		}
		
		return isSuc;
	}
	
	/**
	 * 通过customId查询该客户是否存在，如果存在返回true，否则返回false
	 * 
	 * @param customId
	 * @return
	 */
	public static boolean queryByCustomId(String customId){
		boolean isAdd = true;
		String sqlStr = " select * from mcms_custom_info i where i.custom_id = '"+customId+"'";
		
		try {
			DBHelper dbh = DBHelper.getInstance();
			List list = dbh.query(sqlStr);
			if(list==null || list.size()==0){
				isAdd = false;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return isAdd;
	}
	
	public static boolean addCustomInfo(HttpServletRequest request,String uuid){
		
		boolean isSuc = true;
		request.setAttribute("message", "新增客户信息成功");
		
		//获取参数
		List paramsList = new ArrayList();
		Map mcmsSession = (Map)request.getSession().getAttribute("mcmsSession");
		User user = (User)mcmsSession.get("mcmsUser");
		String userId = user.getId().toString();
		paramsList.add(uuid);
		paramsList = GetParamsUtil.getParams(request, "params_addCustom_dw",paramsList);
		
		paramsList.add(userId);
		paramsList.add(CalendarUtil.getCurrentDateTime());
		paramsList.add(CalendarUtil.getCurrentDateTime());
		paramsList.add(userId);
		
		//参数入库
		String sqlStr = McmsApiConfig.get("sql_addCustom_dw");
		try {
			DBHelper dbh = DBHelper.getInstance();
//			dbh.execute(sqlStr, paramsList);
//			if(list==null || list.size()==0){
//				isSuc = false;
//			}
			int result = dbh.execute(sqlStr,paramsList);
			if(result<1){
				isSuc = false;
				request.setAttribute("message", "新增客户基本信息失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			isSuc = false;
			request.setAttribute("message", "新增客户基本信息失败");
		}
		return isSuc;
	}
	
	/**
	 * 更新当前用户信息
	 * @param request
	 * @param uuid 当前客户id
	 * @return
	 */
	public static boolean updateCustomInfo(HttpServletRequest request,String uuid){
		boolean isSuc = true;
		request.setAttribute("message", "更新客户信息成功");
		
		//获取参数,与insert操作一样
		List paramsList = new ArrayList();
		
		paramsList = GetParamsUtil.getParams(request, "params_addCustom_dw",paramsList);
		
//		paramsList.add(request.getParameter("userName").toString());
//		paramsList.add(CalendarUtil.getCurrentDateTime());
		paramsList.add(CalendarUtil.getCurrentDateTime());
//		paramsList.add(request.getParameter("userName").toString());
		
		paramsList.add(uuid);//更新操作时，最后添加customId
		
		//参数入库，调用update语句
		String sqlStr = McmsApiConfig.get("sql_updateCustom_dw");
		try {
			DBHelper dbh = DBHelper.getInstance();
			int i = dbh.execute(sqlStr, paramsList);
			if(i < 0){
				isSuc = false;
				request.setAttribute("message", "更新客户基本信息失败");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			isSuc = false;
			request.setAttribute("message", "更新客户基本信息失败");
		}
		return isSuc;
	}
}
